﻿using System.Web.Mvc;

namespace System.ComponentModel.DataAnnotations
{
	/// <summary>
	/// For use when something needs to either not exist, or exist at a certain minimum length.
	/// </summary>
	public class MinLengthOrZeroLengthAttribute : ValidationAttribute
	{
		private int _MinLength { get; set; }

		public MinLengthOrZeroLengthAttribute(int MinLength)
		{
			_MinLength = MinLength;
		}

		public override bool IsValid(object value)
		{
			var o = value.ToStringOrEmpty();
			return o.Length == 0 || o.Length >= _MinLength;
		}
	}
}
